//
//  YHEmotionCollectionViewCell.swift
//  XLWB
//
//  Created by key on 16/7/5.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class YHEmotionCollectionViewCell: UICollectionViewCell {
    
    //接收数组数据
    var emotions: [YHEmotionModel]? {
        
        didSet {
            
            //设置数据信息
            //遍历数据数组
            for (i,emotion) in emotions!.enumerate() {
                
                //获取当前button
                let button = emotionButtonArr[i]
                
                //隐藏button
                button.hidden = true
                
                //赋值
                button.emotion = emotion
                
                //判断
                if emotion.isEmoji {
                    //是emoji 表情
                    
                    //显示button
                    button.hidden = false
                    
                    //设置表情数据
                    let title = (emotion.code ?? "").emoji()
                    
                    button.setTitle(title, forState: UIControlState.Normal)
                    
                    //设置图片
                    button.setImage(nil, forState: UIControlState.Normal)
                  
                }else{
                    //是其他两种表情
                    
                    //显示button
                    button.hidden = false
                    
                    //设置button 背景图片
                    let image = UIImage(named: "\(emotion.path ?? "")\(emotion.png ?? "")", inBundle: YHEmotionData.shareEmotion.emotionBundle, compatibleWithTraitCollection: nil)
                    button.setImage(image, forState: UIControlState.Normal)
                    
                    //设置title
                    button.setTitle(nil, forState: UIControlState.Normal)
         
                }
                
            }
            
            
        }
    }
    
    
    //记录创建的button 
    var emotionButtonArr: [YHEmotionButton] = [YHEmotionButton]()
    
    //MARK: - 入口
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置UI
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 设置UI
    func setupUI() {
        
        //加载button
        getEmotionButton()
        
        //添加删除按钮
        contentView.addSubview(deleteButton)
        
        
    }
    
    
    //创建button
    func getEmotionButton() {
        
        //循环
        for _ in 0..<kcellContent {
            
            let button = YHEmotionButton()
            
            button.backgroundColor = self.backgroundColor
            button.titleLabel?.font = UIFont.systemFontOfSize(32)
            
            contentView.addSubview(button)
            
            button.addTarget(self, action: "buttonClick:", forControlEvents: UIControlEvents.TouchUpInside)
            
            emotionButtonArr.append(button)
        
        }
        
    }
    
    //创建删除deleteButton
    lazy var deleteButton: UIButton = {
        
        let del = UIButton()
        
        del.backgroundColor = self.backgroundColor
        
        del.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        
        del.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        
        del.addTarget(self, action: "deleteButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return del
        
    }()
    
    
    
    //设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置表情button frame
        //宽
        let w = (screenW - 10) / CGFloat(kemotionCol)
        
        //高
        let h = (216 - 37 - 20) / CGFloat(kemotionRow)
        
        for (i,button) in emotionButtonArr.enumerate() {

            //x轴
            let x = CGFloat(i % kemotionCol) * w + 5
            
            //y轴
            let y = CGFloat(i / kemotionCol) * h
            
            
            //设置frame
            button.frame = CGRect(x: x, y: y, width: w, height: h)
            
        }
        
        
        
        //设置删除按钮deleteButton frame
        deleteButton.frame = CGRectMake(screenW - 5 - w, CGFloat(kemotionRow - 1) * h, w, h)
        
    }
    
    
    
    //MARK: - button 点击响应事件
    @objc private func deleteButtonClick() {
        
        //发送通知 告知删除一个
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionDeleteNoti, object: nil)
        
    }
    
    @objc private func buttonClick(button: YHEmotionButton) {
        
        //发送通知 添加表情到textView
        NSNotificationCenter.defaultCenter().postNotificationName(EmotionButtonNoti, object: button.emotion)
        
    }
    
    
    
}
